/**
 * COPYRIGHT. Harry Wu 2011. ALL RIGHTS RESERVED.
 * Project: just-core
 * Author: Harry Wu <harrywu304@gmail.com>
 * Created On: Jun 21, 2011 2:47:51 PM
 *
 */
package org.justframework.server;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.justframework.core.helper.ConfigHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * resource server
 * 
 */
public class RestServer {

	/**
	 * logger
	 */
	private static Logger logger = LoggerFactory
			.getLogger(RestServer.class);

	/**
	 * server connectors
	 */
	private List<ServerConnector> serverConnectors;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		logger.info("startup resource server on " + new Date());
		try {
			RestServer mainServer = new RestServer();
			mainServer.initialization();
			mainServer.startup();
		} catch (Exception e) {
			logger.error("startup resource server fail ", e);
		}
	}

	/**
	 * initial server connector list
	 */
	public void initialization() {
		String[] servers = ConfigHelper.getStringArray("servers/server");
		logger.info("servers config:{}",Arrays.asList(servers));
		// initial connector list
		serverConnectors = new ArrayList<ServerConnector>();
		for (String serverStr : servers) {
			String[] serverInfo = serverStr.split(":");
			String schema = serverInfo[0];
			int port = Integer.parseInt(serverInfo[1]);
			//create server connector
			ServerConnector cnt = ServerConnectorFactory.getServerConnector(schema, port);
			serverConnectors.add(cnt);
		}
	}

	/**
	 * startup resource server
	 */
	public void startup() {
		if (serverConnectors != null) {
			for (ServerConnector connector : serverConnectors) {
				connector.startup();
			}
		} else {
			logger.warn("no server connector can be startup!!");
		}
	}
	
	/**
	 * startup resource server
	 */
	public void shutdown() {
		if (serverConnectors != null) {
			for (ServerConnector connector : serverConnectors) {
				connector.shutdown();
			}
		} else {
			logger.warn("no server connector can be shutdown!!");
		}
	}
	
	/**
	 * get binding server connectors
	 * @return
	 */
	public List<ServerConnector> getServerConnectors(){
		return serverConnectors;
	}

}
